package com.zego.wawaji.ui.Fragment.store;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.noble.library.observer.MsgMgr;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.Fragment.BaseFragment;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.base.recycler.xRecyclerView.ProgressStyle;
import com.zego.wawaji.ui.base.recycler.xRecyclerView.XRecyclerView;
import com.zego.wawaji.ui.bean.user.UserDetail;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.store.adapter.StoreListAdapter;
import com.zego.wawaji.ui.store.bean.Store;
import com.zego.wawaji.ui.store.bean.StoreList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Su on 2018/1/18.
 */

public class StoreFragment extends BaseFragment implements PObserver, RequestComplete, View.OnClickListener, XRecyclerView.LoadingListener {
    private CustomRecyclerView statusLayout;
    private XRecyclerView recyclerView;
    private TextView coinRemain;

    private StoreListAdapter adapter;
    private List<Store> list = new ArrayList<>();
    private int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.store_activity);
        initView();
        reqData();
        return getContentView();
    }

    private void reqData() {
        ModuleMgr.getWawaMgr().reqGoodsList(page, this);
    }

    private void initView() {
        setTitle("兑换商城");
        initRecyclerView();

        View header = LayoutInflater.from(getActivity()).inflate(R.layout.store_header, null);
        coinRemain = (TextView) header.findViewById(R.id.coin_remain);
        coinRemain.setText(String.valueOf(ModuleMgr.getCenterMgr().getMyInfo().getWwCoinCz()));
        header.findViewById(R.id.my_order).setOnClickListener(this);
        header.findViewById(R.id.exchange_explain).setOnClickListener(this);
        header.findViewById(R.id.charge_layout).setOnClickListener(this);
        recyclerView.addHeaderView(header);

        MsgMgr.getInstance().attach(this);
    }

    private void initRecyclerView() {
        statusLayout = (CustomRecyclerView) findViewById(R.id.statusLayout);
        statusLayout.showXrecyclerView();
        recyclerView = statusLayout.getXRecyclerView();
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallPulse);
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallPulse);
        recyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
        recyclerView.setLoadingListener(this);

        adapter = new StoreListAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);
    }

    public void refreshView() {
        UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        if (coinRemain != null) {
            coinRemain.setText(String.valueOf(userDetail.getWwCoinCz()));
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        try {
            if (response.getUrlParam() == UrlParam.reqGoodsList) {
                if (page == 1) {
                    recyclerView.refreshComplete();
                } else {
                    recyclerView.loadMoreComplete();
                }
                setData(response);
            }
        } catch (Exception e) {
        }
    }

    private void setData(HttpResponse response) {
        // 请求失败
        if (!response.isOk()) {
            if (list.size() != 0) {
                if (list.size() < 100) {
                    recyclerView.setLoadingMoreEnabled(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showXrecyclerView();
                return;
            }
            statusLayout.showNoData();
            return;
        }

        // 非缓存
        if (!response.isCache()) {
            StoreList store = (StoreList) response.getBaseData();

            if (store.getStoreList().size() != 0) {
                //如果请求的第一页 应该把之前的数据都清掉，把返回的数据作为第一页
                if (page == 1) {
                    if (list.size() != 0) {
                        list.clear();
                    }
                    page = 1;
                }

                list.addAll(store.getStoreList());
                if (list.size() < 100) {
                    recyclerView.setLoadingMoreEnabled(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showXrecyclerView();
            } else {
                if (page == 1) {
                    statusLayout.showNoData();
                } else {
                    recyclerView.setLoadingMoreEnabled(false);
                }
            }
            return;
        }

        // 缓存
        if (page == 1) {
            StoreList store = (StoreList) response.getBaseData();

            if (store != null && store.getStoreList().size() != 0) {
                if (list.size() != 0) {
                    list.clear();
                }
                list.addAll(store.getStoreList());
                if (list.size() < 100) {
                    recyclerView.setLoadingMoreEnabled(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showXrecyclerView();
            }
        }
    }

    public void release() {
        if (recyclerView != null) {
            recyclerView.destroy();
            recyclerView = null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_order:
                UIShow.showStoreOrderActivity(getActivity());
                break;

            case R.id.exchange_explain:
                UIShow.showStoreDeclareActivity(getActivity());
                break;

            case R.id.charge_layout:
                PickerDialogUtil.showPayDialog(getActivity());
                break;

        }
    }

    @Override
    public void onRefresh() {
        recyclerView.setLoadingMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        page = 1;
        reqData();
    }

    @Override
    public void onLoadMore() {
        page++;
        reqData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                refreshView();
                break;
        }
    }
}